package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.Employee;
import com.itheima.reggie.service.EmployeeService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.time.LocalDateTime;

@Slf4j
@RestController
@RequestMapping("/employee")
public class EmployeeController {
    @Autowired
    private EmployeeService employeeService;

    /**
     * 登录后台查询
     * @param request
     * @param employee
     * @return
     */
    @PostMapping("/login")
    public R<Employee> login(HttpServletRequest request, @RequestBody Employee employee){
        //1.将页面提交的密码password进行md5加密处理
        String password= employee.getPassword();
        password=DigestUtils.md5DigestAsHex(password.getBytes());
        //2.根据页面提交的用户名和密码查询数据库
        LambdaQueryWrapper<Employee> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Employee::getUsername,employee.getUsername()).eq(Employee::getPassword,password);
        Employee emp = employeeService.getOne(lambdaQueryWrapper);
        //3.判断是否查到数据 没有则返回登陆失败
        if(emp==null){
            return R.error("登录失败");
        }
        //4.判断状态是否被禁用
        if (emp.getStatus()==0){
            return R.error("您已被禁用");
        }
        //5.登录成功则将员工id保存到session中并返回登录成功的结果
        request.getSession().setAttribute("employee",emp.getId());
//        System.out.println(emp.getId());
        return R.success(emp);
    }

    /**
     * 退出
     * @param session
     * @return
     */
    @PostMapping("/logout")
    public R<String> logout(HttpSession session){
        session.invalidate();//将session自杀以完成退出
        return R.success("退出成功");
    }

    /**
     * 添加员工数据
     * @param request
     * @param employee
     * @return
     */
    @PostMapping
    public R<String> save(HttpServletRequest request, @RequestBody Employee employee){
        //1.指定默认密码并且md5加密处理
        employee.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes()));
        //2.设置创建的时间
       /* employee.setCreateTime(LocalDateTime.now());
        //3.设置更新时间
        employee.setUpdateTime(LocalDateTime.now());
        //4.获取session域中的employee的id
        Long id = (Long) request.getSession().getAttribute("employee");
        //5.设置添加人的id和修改人的id
        employee.setCreateUser(id);
        employee.setUpdateUser(id);*/
        //6.将对象添加到数据并返回结果
        employeeService.save(employee);
        return R.success("添加成功！");
    }

    /**
     * 员工信息分页查询
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping("/page")
    public R<Page> page(int page,int pageSize,String name){
        //构造分页构造器
        Page pageInfo=new Page(page,pageSize);
        //构造条件构造器
        LambdaQueryWrapper<Employee> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.like(StringUtils.isNotEmpty(name),Employee::getName,name);
        lambdaQueryWrapper.orderByDesc(Employee::getUpdateTime);
        employeeService.page(pageInfo, lambdaQueryWrapper);
        return R.success(pageInfo);
    }

    /**
     * 根据员工id修改员工信息
     * @param request
     * @param employee
     * @return
     */
    @PutMapping
    public R<String> update(HttpServletRequest request,@RequestBody Employee employee){
       /* Long empid = (Long) request.getSession().getAttribute("employee");
        employee.setUpdateTime(LocalDateTime.now());
        employee.setUpdateUser(empid);*/
        employeeService.updateById(employee);
        return R.success("员工修改成功");
    }

    /**
     * 根据请求发送来的id查询数据
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R<Employee> updateById(@PathVariable Long id){
        Employee emp = employeeService.getById(id);
        if(emp!=null){
            return R.success(emp);
        }
            return R.error("修改失败");
    }}
